//app.js
var request = require('./utils/request.js');
var appkey = require('./appkey.js');
var config = require('./config.js');
var login = require('./actions/login.js');

App({
  onLaunch: function (options) {
    //入场模式判断
    if (options.scene == 1044) {
      this.globalData.showHome = true;
    } else {
      this.globalData.showHome = false;
    }
   
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    });

    this.getPhoneInfo();

  },

  globalData: {
    userInfo: undefined,
    user: {},//系统中用户信息
    theme: {
      mainColor: "#fc8641",
    },
    shopId: 0,
    shopAlias: 'weima',
    shopInfo: {},
    phoneInfo: {},//设备信息
    isIpx: false,//iPhone X 单独判断
    appId: 'wx4e80681be63b59dc',
    showHome: false,//活动界面显示home 
  },


  getAuthMoal: function () {
    let self = this;
    wx.showModal({
      title: '警告',
      content: '没有授权部分功能将无法正常使用',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#f00',
      confirmText: '去授权',
      success: function (res) {
        if (res.confirm) {
          wx.openSetting({
            success: function (res) {
              let sets = res.authSetting;
              if (sets['scope.userInfo']) {
            
                wx.getUserInfo({
                  success: res => {
                    console.log(res);
                    self.globalData.userInfo = res.userInfo;
                    login.lmmLogin((res) => {
                      self.globalData.user = res;
                    }, self.globalData.shopId, self.globalData.userInfo);
                  }});

                
              }
            }
          });
        }
      }
    })
  },


  getShopDetail: function () {
    var self = this;
    return new Promise((resoleve, reject) => {
      let url = config.getShopDetailUrl;
      let params = {
        alias: self.globalData.shopAlias
      }
      request.requestPost(url, params)
        .then(data => {
          console.log('getShopDetail')
          console.log(data);
          self.globalData.shopId = data.shop_id;
          self.globalData.shopInfo = data;
          resoleve(data);
        }).catch(msg => {
          console.log(msg);
          reject(msg);
        });
    });
  },

  /**
   * 获取设备信息
   */
  getPhoneInfo: function () {
    var self = this;
    wx.getSystemInfo({
      success: function (res) {
        self.globalData.phoneInfo = res;
        var reg = RegExp(/iPhone X/);
        self.globalData.isIpx = res.model.match(reg);
        console.log(self.globalData.isIpx);
        console.log(res.model)
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        console.log(res.platform)
      }
    });
  },


})